import { reject } from "async";
import axios from "axios"
import { response } from "express";
import QS from "qs"
import { Toast } from "vant"
import store from "../vuex/store"

//多环境开发，环境的切换
if (process.env.NODE_ENV == 'development') {
    axios.defaults.baseURL = '/api';
} else if (process.env.NODE_ENV == 'debug') {
    axios.defaults.baseURL = '';
} else if (process.env.NODE_ENV = 'production') {
    axios.defaults.baseURL = "http://172.16.14.67:8080"
}

axios.defaults.timeout = 1000;

//post请求头的设置
//post请求的时候，需要加上一个请求头
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

//请求拦截 作用1.有些请求是登录之后才能访问2.post请求时，序列化我们提交的数据。
axios.interceptors.request.use(
    config => {
        const token = store.state.token;
        token && (config.headers.Authorization = token);
        return config;
    },
    error => {
        return Promise.error(error);
    }
)
axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response)
        }
    },
    error => {
        if (error.response.status) {
            switch (error.response.status) {
                case 401:
                    router.replace({
                        path: "/login",
                        query: {
                            redirect: router.currentRoute.fullPath
                        }
                    });
                    break;
                case 403:
                    Toast({
                        message: '登录过期，请重新登录',
                        duration: 1000,
                        forbidClick: true
                    });
                    localStorage.removeItem('token');
                    store.commit("loginSuccess", null);
                    setTimeout(() => {
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                    }, 1000);
                    break;
                case 404:
                    Toast({
                        message: "网络请求不存在",
                        duration: 1500,
                        forbidClick: true
                    })
                    break;
                default:
                    Toast({
                        massage: error.response.data.message,
                        duration: 1500,
                        forbidClick: true
                    })
            }
            return Promise.reject(error.response)

        }
    }
)
/**
 * @method get 对应get请求
 * @param {String} url 
 * @param {JSON} params 
 * @returns 
 */
export function get(url,params) {
    return new Promise((resolve,reject)=>{
        axios.get(url,{
            param:params
        }).then(res=>{
            resolve(res.data)
        }).catch(err =>{
            reject(err.data)
        })
    })
}
//post方法：必须要使用对提交从参数对象进行序列化的操作，使用qs模块来序列化参数。没有序列化，后台拿不到数据
/**
 * @method post 对应post请求
 * @param {String} url 
 * @param {JSON} params 
 * @returns 
 */
export function post(url,params) {
    return new Promise((resolve,reject) =>{
        axios.post(url,QS.stringify(params)).then(res=>{
            resolve(res.data)
        }).catch(err =>{
            reject(err.data)
        })
    })
    
}
